package woa.linkedlist;

/**
 * 删除链表的倒数第N个节点
 * 给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。
 *
 * @author wangpeng
 * @date 2021/11/20
 */
public class RemoveNthFromEndProblem {

    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode sentry = new ListNode(0, head);
        ListNode slow = sentry, fast = sentry;
        while (fast.next != null) {
            if (n > 0) {
                fast = fast.next;
                n--;
            } else {
                slow = slow.next;
                fast = fast.next;
            }
        }
        slow.next = slow.next.next;
        return sentry.next;
    }

    public static void main(String[] args) {

    }
}
